import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        String[] str = reader.readLine().split(" ");
        String s1 = str[0], s2 = str[1];
        int prev1 = -1, prev2 = -1, ret = 0x3f3f3f3f;
        for(int i = 0; i < n; i++)
        {
            String s = reader.readLine();
            if(s.equals(s1))
            {
                if(prev2 != -1)
                {
                    ret = Math.min(ret, i - prev2);
                }
                prev1 = i;
            }
            else if(s.equals(s2))
            {
                if(prev1 != -1)
                {
                    ret = Math.min(ret, i - prev1);
                }
                prev2 = i;
            }
        }
        System.out.println(ret == 0x3f3f3f3f ? -1 : ret);
    }

}
